/**
 * Package	: com.coship.adstorm.service.impl
 * File		: PositionInfoServiceImpl.java
 * Company 	: Shenzhen Coship Electronics CO., LTD.
 * Version  : v1.0
 * Description 	: <Detail Description for package>
 *
 * Copyright (c) 2003-2009 Shenzhen Coship Electronics CO., LTD.
 * Rainbow Bldg.,North,Hi-Tech Industrial Park,Nanshan District,Shenzhen,China,P.C.518057.
 * All rights reserved.
 * The contents of this file are confidential and proprietary to COSHIP.
 * Copying is explicitly prohibited without the express permission of COSHIP.
 *
 * Create Date	: Apr 27, 2010
 * Create By    : 904120
 * History :$
 */
package com.coship.vasms.adstorm.service.impl;

import com.coship.vasms.adstorm.common.DateHelper;
import com.coship.vasms.adstorm.dao.PositionInfoDao;
import com.coship.vasms.adstorm.model.PositionInfo;
import com.coship.vasms.adstorm.service.ADArrangeService;
import com.coship.vasms.adstorm.service.PositionBasicInfoService;
import com.coship.vasms.adstorm.service.PositionInfoService;
import com.coship.vasms.adstorm.webservice.WebServiceConstant;
import com.coship.vasms.adstorm.webservice.position.Response;
import com.coship.vasms.adstorm.webservice.position.positioninfo.vo.PositionInfoReq;

/**
 * 
 * File Name : PositionInfoServiceImpl.java
 * 
 * @Description : 广告位service实现类
 * @author 904120
 */
public class PositionInfoServiceImpl implements PositionInfoService {
	// get set 注入
	private PositionInfoDao positionInfoDao;
	private PositionBasicInfoService positionBasicInfoService;
	private ADArrangeService adArrangeService;

	/**
	 * @return the positionBasicInfoService
	 */
	public PositionBasicInfoService getPositionBasicInfoService() {
		return positionBasicInfoService;
	}

	/**
	 * @param positionBasicInfoService the positionBasicInfoService to set
	 */
	public void setPositionBasicInfoService(
			PositionBasicInfoService positionBasicInfoService) {
		this.positionBasicInfoService = positionBasicInfoService;
	}

	/**
	 * @return the adArrangeService
	 */
	public ADArrangeService getAdArrangeService() {
		return adArrangeService;
	}

	/**
	 * @param adArrangeService the adArrangeService to set
	 */
	public void setAdArrangeService(ADArrangeService adArrangeService) {
		this.adArrangeService = adArrangeService;
	}

	@Override
	public Response synPositionInfo(PositionInfoReq requset) {
		// TODO Auto-generated method stub
		PositionInfo info = buildPositionInfo(requset);
		if (info != null) {
			Response response= null;
			if (WebServiceConstant.POSITIONBASICINFO_OP_ADD
					.equalsIgnoreCase(requset.getOperation())) {// 新增
				response= addPositionInfo(info);
				return response;
			} else if (WebServiceConstant.POSITIONBASICINFO_OP_MODIFY
					.equalsIgnoreCase(requset.getOperation())) {// 修改
				response= modifyPositionInfo(info);
				return response;
			} else if (WebServiceConstant.POSITIONBASICINFO_OP_DELETE
					.equalsIgnoreCase(requset.getOperation())) {// 删除
				response= delPositionInfo(info);
				return response;
			} else {
				throw new RuntimeException("不支持的广告位附加信息操作");
			}
		
		}
		return null;
	}

	/**
	 * 
	 * Description : 取请求参数中的信息
	 * 
	 * @param requset
	 * @return
	 * 
	 */
	private PositionInfo buildPositionInfo(PositionInfoReq requset) {
		PositionInfo info = new PositionInfo();
		info.setPositionInfoCode(requset.getPositionCode());
		info.setPositionBasicCode(requset.getPositionBasicCode());
		info.setPositionType(Integer.parseInt(requset.getPositionType()));
		if (WebServiceConstant.POSITIONINFO_POSITIONTYPE_SERVICE
				.equalsIgnoreCase(requset.getPositionType())) {
			// 业务信息
			info.setServiceCode(requset.getServiceCode());
			info.setServiceName(requset.getServiceName());
		} else if (WebServiceConstant.POSITIONINFO_POSITIONTYPE_COLUM
				.equalsIgnoreCase(requset.getPositionType())) {
			// 业务信息
			info.setServiceCode(requset.getServiceCode());
			info.setServiceName(requset.getServiceName());
			// 栏目信息
			info.setColumnCode(requset.getColumnCode());
			info.setColumnName(requset.getColumnName());			
		} else if (WebServiceConstant.POSITIONINFO_POSITIONTYPE_CONTENT
				.equalsIgnoreCase(requset.getPositionType())) {
			// 业务信息
			info.setServiceCode(requset.getServiceCode());
			info.setServiceName(requset.getServiceName());
			// 栏目信息
			info.setColumnCode(requset.getColumnCode());
			info.setColumnName(requset.getColumnName());
			// 文章&&内容信息
			info.setContentCode(requset.getContentCode());
			info.setContentName(requset.getContentName());
		}
		// 搜索信息
		info.setIndexContent(requset.getIndexContent());
		return info;
	}

	/**
	 * 
	 * Description : 新增广告位附加信息
	 * 
	 * @param positionInfo
	 *            附加位数据
	 * @return
	 * 
	 */
	private Response addPositionInfo(PositionInfo positionInfo) {
		Response response= null;
		//1.是否已经存在广告位基本信息编码
		boolean isExisted=positionBasicInfoService.isExistPositionBasicCode(positionInfo.getPositionBasicCode());
		if(!isExisted){
			response=new Response(WebServiceConstant.RESPONSE_ERROR_BASICCODE, "广告位基本信息编码不存在");
			return response;
		}
		//2.是否已经存在广告位附加信息编码
		isExisted=isExistedPositionInfoCode(positionInfo.getPositionInfoCode());
		if(isExisted){
			response=new Response(WebServiceConstant.RESPONSE_ERROR_POSITIONCODE, "广告位附加信息编码已存在");
			return response;
		}
		//插入
		positionInfo.setCreateTime(DateHelper.getCurrentSecond());//设置创建时间
		positionInfoDao.addPositionInfo(positionInfo);
		response=new Response(WebServiceConstant.RESPONSE_SUCCESS,null);
		return response;
		
	}

	/**
	 * 
	 * Description : 修改广告位附加信息
	 * 
	 * @param positionInfo
	 *            附加位数据
	 * @return
	 * 
	 */
	private Response modifyPositionInfo(PositionInfo positionInfo) {
		Response response= null;
		//1.是否已经存在广告位基本信息编码
		boolean isExisted=positionBasicInfoService.isExistPositionBasicCode(positionInfo.getPositionBasicCode());
		if(!isExisted){
			response=new Response(WebServiceConstant.RESPONSE_ERROR_BASICCODE, "广告位基本信息编码不存在");
			return response;
		}
		//2.是否已经存在广告位附加信息编码
		isExisted=isExistedPositionInfoCode(positionInfo.getPositionInfoCode());
		if(!isExisted){
			response=new Response(WebServiceConstant.RESPONSE_ERROR_POSITIONCODE, "广告位附加信息编码不存在");
			return response;
		}
		positionInfo.setLastUpdateTime(DateHelper.getCurrentSecond());//设置修改时间
		//修改
		positionInfo.setState(WebServiceConstant.POSITIONINFO_STATE_Y);//有效	
		positionInfoDao.updatePositionInfo(positionInfo);
		//修改排期状态
		adArrangeService.updateArrangeByBasicCode(positionInfo.getPositionInfoCode(), WebServiceConstant.ARRANGE_STATE_N);
		response=new Response(WebServiceConstant.RESPONSE_SUCCESS,null);
		return response;
		
	}

	/**
	 * 
	 * Description : 删除广告位附加信息
	 * 
	 * @param positionInfo
	 *            附加位数据
	 * @return
	 * 
	 */
	private Response delPositionInfo(PositionInfo positionInfo) {
		Response response= null;
		//1.是否已经存在广告位基本信息编码
		boolean isExisted=positionBasicInfoService.isExistPositionBasicCode(positionInfo.getPositionBasicCode());
		if(!isExisted){
			response=new Response(WebServiceConstant.RESPONSE_ERROR_BASICCODE, "广告位基本信息编码不存在");
			return response;
		}
		//2.是否已经存在广告位附加信息编码
		isExisted=isExistedPositionInfoCode(positionInfo.getPositionInfoCode());
		if(!isExisted){
			response=new Response(WebServiceConstant.RESPONSE_ERROR_POSITIONCODE, "广告位附加信息编码不存在");
			return response;
		}
		//修改
		positionInfo.setLastUpdateTime(DateHelper.getCurrentSecond());//设置修改时间
		positionInfo.setState(WebServiceConstant.POSITIONINFO_STATE_N);
		positionInfoDao.updatePositionInfo(positionInfo);
		//修改排期状态
		adArrangeService.updateArrangeByBasicCode(positionInfo.getPositionInfoCode(), WebServiceConstant.ARRANGE_STATE_N);
		response=new Response(WebServiceConstant.RESPONSE_SUCCESS,null);
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.coship.adstorm.service.PositionInfoService#updatePositionInfo(java.lang.String,
	 *      int)
	 */
	@Override
	public int updatePositionInfoState(String positionBasicCode, int state) {
		// TODO Auto-generated method stub
		PositionInfo positionInfo = new PositionInfo();
		//设置更新时间
		positionInfo.setLastUpdateTime(DateHelper.getCurrentSecond());
		positionInfo.setPositionBasicCode(positionBasicCode);
		positionInfo.setState(state);
		return positionInfoDao.updatePositionInfoState(positionInfo);
	}

	/**
	 * @return the positionInfoDao
	 */
	public PositionInfoDao getPositionInfoDao() {
		return positionInfoDao;
	}

	/**
	 * @param positionInfoDao
	 *            the positionInfoDao to set
	 */
	public void setPositionInfoDao(PositionInfoDao positionInfoDao) {
		this.positionInfoDao = positionInfoDao;
	}

	@Override
	public boolean isExistedPositionInfoCode(String positionCode) {
		// TODO Auto-generated method stub
		int count=-1;
		PositionInfo positionInfo= new PositionInfo();
		positionInfo.setPositionInfoCode(positionCode);
		count=positionInfoDao.countPositionInfoByCondition(positionInfo);
		return count>0?true:false;
	}

}
